﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;
using DTO;

namespace DAO
{
    public class UserDAO:AbstractDAO
    {
        public static UserDTO DangNhap(string username, string password)
        {
            // Mo ket noi va tao ra user dang nhap
            OleDbConnection connect = null;
            UserDTO user = null;

            try
            {
                connect = MoKetNoi();
                string chuoiLenh = "SELECT ID,UserName,PassWord,User_Right FROM [User] WHERE UserName=@Username AND Password=@Password";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, connect);

                OleDbParameter thamSo;
                thamSo = new OleDbParameter("@Username", OleDbType.VarChar);
                thamSo.Value = username;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@Password", OleDbType.VarChar);
                thamSo.Value = password;
                lenh.Parameters.Add(thamSo);

                OleDbDataReader boDoc = lenh.ExecuteReader();
                while(boDoc.Read())
                {
                    user = new UserDTO();
                    if(boDoc.GetInt32(0) != 0)
                        user.UserID = boDoc.GetInt32(0);
                    if (!boDoc.IsDBNull(1))
                        user.UserName = boDoc.GetString(1);
                    if (!boDoc.IsDBNull(2))
                        user.PassWord = boDoc.GetString(2);
                    if (!boDoc.IsDBNull(3))
                        user.UserRight = boDoc.GetString(3);
                    return user;
                } // ket thuc while
            } // ket thuc try
            catch (Exception ex)
            {
                return user;
            } // ket thuc catch
            finally
            {
                if (connect != null && connect.State == System.Data.ConnectionState.Open)
                    connect.Close();
            } // ket thuc finally

            // tra ve user
            return user;
        } // ket thuc phuong thuc DangNhap
    }
}
